import store from './store';
import router from './router';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import { getToken,getLang } from './utils/auth';
NProgress.inc(0.2);
NProgress.configure({easing: 'ease', speed: 500, showSpinner: false});

const educationModules = /\/education\/(\d{1,})\//

router.beforeEach((to ,from, next) => {
    if(to.meta && to.meta.title){
      document.title = to.meta.title + " | 灯塔学院"
    }
    NProgress.start();
    // 获取到token,即已登录
    if(getToken()){
        if(to.path === '/login'){
            // 去登录页直接弹回主页
            next({path:'/'});
            NProgress.done();
        }else{
            // 去的不是登入页面(教育模块)
            if(educationModules.test(to.path)){
                let id = Number(RegExp.$1)
                store.dispatch('XisBuy',{
                    objId:id,
                    objType:"COURSE"
                })
                next()
                NProgress.done();
            }else{
                next();
                NProgress.done();
            }
            
        }
    }
    // 没有token,即未登录
    else{
        if(to.meta.requireAuth){
            // 需要登录权限的路由
            next({
                path:'/login',
                query:{redirect:to.fullPath}
            });
            NProgress.done();
        }else{
            // 不需要登录权限的路由
            next();
            NProgress.done();
        }
    }
});

router.afterEach(() => {
    NProgress.done();
})